package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.mapper.*;
import com.jgsu.farm.mapper.sql.DiseaseStressMapperSql;
import com.jgsu.farm.mapper.sql.MatchImageMapperSql;
import com.jgsu.farm.mapper.sql.StressCategoryMapperSql;
import com.jgsu.farm.service.DiseaseStressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class DiseaseStressServiceImpl implements DiseaseStressService {
    @Autowired
    DiseaseStressMapper diseaseStressMapper;
    @Autowired
    DiseaseStressMapperSql diseaseStressMapperSql;
    @Autowired
    StressCategoryMapper stressCategoryMapper;
    @Autowired
    PlantMapper plantMapper;
    @Autowired
    MatchImageMapperSql matchImageMapperSql;
    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public void add(DiseaseStress diseaseStress) {
        StressCategory stressCategory = stressCategoryMapper.selectByPrimaryKey(diseaseStress.getStresstype());
        Plant plant = plantMapper.selectByPrimaryKey(diseaseStress.getCropid());
        List<MatchImage> pathoImage = matchImageMapperSql.selectByMatchId(diseaseStress.getPathoimage());
        List<MatchImage> symptomImage = matchImageMapperSql.selectByMatchId(diseaseStress.getSymptomimage());
        if (stressCategory != null && plant != null && !pathoImage.isEmpty() && !symptomImage.isEmpty()) {
            diseaseStressMapper.insert(diseaseStress);
        } else {
            throw new DataAddException("604", "不存在stressId或plantID或pathoImage或symptomImage");
        }
    }

    @Override
    public void delete(Integer diseaseid) {
        int i = diseaseStressMapper.deleteByPrimaryKey(diseaseid);
        if (i == 0){
            throw new DataNotExistException("603", "不存在该diseaseid，病害胁迫删除失败");
        }
    }

    @Override
    public PageInfo<DiseaseStress> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        // 查询redis中是否存在
               List<DiseaseStress> diseaseStressList = (List<DiseaseStress>) redisTemplate.opsForValue().get("PestStresses");
               // 双重检测锁机制，解决缓存穿透问题，从而处理高并发要求
               if (null == diseaseStressList) {
                   synchronized (this) {
                       diseaseStressList = (List<DiseaseStress>) redisTemplate.opsForValue().get("PestStresses");
                       if (null == diseaseStressList) {
                           diseaseStressList = diseaseStressMapperSql.selectAll();
                           log.info("从数据库中查询diseaseStressList。。。。");
                           redisTemplate.opsForValue().set("DiseaseStressList", diseaseStressList);
                           log.info("将从数据库中查询的diseaseStressList数据存入redis。。。。");
                       }
                   }
               }
        PageInfo<DiseaseStress> pageInfoDiseaseStressList = new PageInfo<DiseaseStress>(diseaseStressList);
        return pageInfoDiseaseStressList;
    }


    @Override
    public void update(DiseaseStress diseaseStress) {
        DiseaseStress diseaseStressId = diseaseStressMapper.selectByPrimaryKey(diseaseStress.getDiseaseid());
        if (diseaseStressId != null) {
            StressCategory stressCategory = stressCategoryMapper.selectByPrimaryKey(diseaseStress.getStresstype());
            Plant plant = plantMapper.selectByPrimaryKey(diseaseStress.getCropid());
            List<MatchImage> pathoImage = matchImageMapperSql.selectByMatchId(diseaseStress.getPathoimage());
            List<MatchImage> symptomImage = matchImageMapperSql.selectByMatchId(diseaseStress.getSymptomimage());
            if (stressCategory != null && plant != null && !pathoImage.isEmpty() && !symptomImage.isEmpty()) {
            diseaseStressMapper.updateByPrimaryKey(diseaseStress);
        } else {
            throw new DataNotExistException("603", "不存在stressId或plantID或pathoImage或symptomImage");
        }
        }else {
            throw new DataNotExistException("603", "不存在stressId");
        }
    }
}
